#!/bin/ash
#Barry Kauler 2012
#GPL v3 (/usr/share/doc/legal)
#120202 internationalized. add udf f.s.
#131130 zigbert: gui (gtkdialog) improvements.
#150203 ASRI: add refresh button

export TEXTDOMAIN=partview
export OUTPUT_CHARSET=UTF-8

/usr/lib/gtkdialog/box_splash -close never -fontsize large -text "$(gettext 'Please wait, probing drives...')" &
yPID=$!

#added stuff 01micko 2010-12-02
. /etc/DISTRO_SPECS
. /etc/rc.d/functions_x #fx_* functions

KERNEL=`uname -r`
PUPVER=${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}

PARTSLIST="`probepart -k -extra-info | grep -E 'udf|iso9660|f2fs|ext2|ext3|ext4|reiserfs|btrfs|minix|msdos|vfat|exfat|ntfs'`"

COUNT=0
S='<window title="Partview" icon-name="gtk-harddisk">
 <vbox space-expand="true" space-fill="true">
   '"`/usr/lib/gtkdialog/xml_info fixed "partition.svg" 60 "<b>Puppy Linux</b>" "$(gettext 'Version'): $PUPVER" "$(gettext 'Kernel'): $KERNEL"`"' 
 <vbox space-expand="true" space-fill="true">
   <vbox scrollable="true" border-width="10">'

while IFS="|" read -r ADEVNAME AFS ASIZE ALABEL AMNT_STATUS
do
	MNTSTATUS='(currently mounted)'
	ADEVNAME="${ADEVNAME#/dev/}" #remove leading '/dev/'
	if fx_drv_is_optical $ADEVNAME ; then
		ASIZE=4300000
		dvd+rw-mediainfo /dev/${ADEVNAME} 2>&1 | grep -E 'non-DVD|not a DVD' && ASIZE=700000
	fi
	AFPATTERN="^/dev/$ADEVNAME "
	AUSED=`df -k | grep "$AFPATTERN" | head -n 1 | tr -s " " | cut -f 3 -d " "`
	[ "$AUSED" ] && [ "$(echo -n "$AFPATTERN" | grep \/luks)" != "" ]  && ADEVNAME="${ADEVNAME#mapper/}"
	if [ ! "$AUSED" ];then
		MNTSTATUS='(not mounted)'
		mkdir -p /mnt/$ADEVNAME
		mount -t $AFS /dev/$ADEVNAME /mnt/$ADEVNAME > /dev/null 2>&1 || continue
		AUSED=`df -k | grep "$AFPATTERN" | head -n 1 | tr -s " " | cut -f 3 -d " "`
		umount /dev/$ADEVNAME
	fi

	ATAG="$ADEVNAME [${AFS}]"
	if [ "$ALABEL" ] ; then
		ATAG="$ADEVNAME [${AFS}] (${ALABEL})"
	fi
 
	AFREE=$(($ASIZE - $AUSED))
	#process size...
	ONEFREE=$( fx_format_bytes $((1024 * $AFREE)) )
	ONESIZE=$( fx_format_bytes $((1024 * $ASIZE)) )
	. /usr/lib/gtkdialog/svg_bar 250 "$(($AUSED*250/$ASIZE))" "${ONESIZE} / ${ONEFREE} $(gettext 'free')"  > /tmp/partview_${COUNT}.svg
	S=$S'<hbox>
  <text><label>'"${ATAG}"'</label></text>
  <pixmap><input file>/tmp/partview_'${COUNT}'.svg</input></pixmap>
 </hbox>'
 COUNT=$(($COUNT+1))
done <<EOF
$PARTSLIST
EOF

export PartView=$S'</vbox></vbox>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Refresh")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon refresh`"'
      <action>partview &</action>
      <action>exit:QUIT</action>
    </button>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <action>exit:OK</action>
    </button>
    '"`/usr/lib/gtkdialog/xml_scalegrip`"'
  </hbox>
</vbox>
</window>'
. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
kill $yPID
gtkdialog -p PartView --geometry=500x400

###END###
